<?php
class system_loader {
	protected static $_instance = null;
	/**
	 * @return system_loader
	 */
	public static function getInstance() {
		if(null===self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	protected $_baseDir = null;
	public function setBaseDir($dir) {
		$this->_baseDir = $dir;
		return $this;
	}
	public function getBaseDir() {
		return $this->_baseDir;
	}
	
	protected function __construct() {
		spl_autoload_register(__CLASS__.'::autoload');
	}
	
	static public function autoload($class) {
		$self = self::getInstance();
		$self->loadClass($class);
	}
	
	public function loadClass($class) {
		$fileName = str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
		$this->loadFile($fileName);
	}
	
	public function classExists($class) {
		if($this->classFileExists($class)) {
			return class_exists($class);
		}
		return false;
	}
	
	public function classFileExists($class) {
		$file = str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
		return file_exists($this->getBaseDir().DIRECTORY_SEPARATOR.$file);
	}
	
	protected $loaders = array();
	public function loadFile($filename, $once = true) {
		$this->loaders[] = $filename;
		$file = $this->getBaseDir().DIRECTORY_SEPARATOR.$filename;
		if(!file_exists($file)) {
			throw new Exception('The file "'.$file.'" can not be found!');
		}
		
		if($once) {
			require_once $file;
		} else {
			require $file;
		}
	}
	
	public function getClassAutoloaders($class) {
		$autoloaders = array();		
		return $autoloaders;
	}
}